package com.suse.service.user.controller.admin;

import com.suse.common.base.model.param.BasePageQueryParam;
import com.suse.common.config.response.R;
import com.suse.common.config.response.SimpleCode;
import com.suse.service.user.entity.Menu;
import com.suse.service.user.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-10-28
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/dev-api/admin/user/menu")
public class AdminMenuController {

    @Resource
    private MenuService menuService;

    @ApiOperation(value = "查询菜单分页")
    @PostMapping("/page")
    public R page(@RequestBody BasePageQueryParam pageQueryParam) {
        return menuService.page(pageQueryParam);
    }

    @ApiOperation(value = "查询菜单列表")
    @GetMapping("/list")
    public R list() {
        return R.success(menuService.list());
    }

    @ApiOperation(value = "添加菜单")
    @PutMapping
    public R save(@RequestBody Menu menu) {
        boolean save = menuService.save(menu);
        if (save) {
            return R.success(SimpleCode.SAVE_SUCCESS);
        }else {
            return R.success(SimpleCode.SAVE_ERROR);
        }
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping
    public R update(@RequestBody Menu menu) {
        boolean updateById = menuService.updateById(menu);
        if (updateById) {
            return R.success(SimpleCode.UPDATE_SUCCESS);
        }else {
            return R.success(SimpleCode.UPDATE_ERROR);
        }
    }

    @ApiOperation(value = "删除菜单(逻辑删除)")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable("id") String id) {
        boolean removeById = menuService.removeById(id);
        if (removeById) {
            return R.success(SimpleCode.REMOVE_SUCCESS);
        }else {
            return R.error(SimpleCode.REMOVE_ERROR);
        }
    }

    @ApiOperation(value = "批量删除菜单(逻辑删除)")
    @DeleteMapping
    public R remove(@RequestBody List<String> ids) {
        boolean removeById = menuService.removeByIds(ids);
        if (removeById) {
            return R.success(SimpleCode.REMOVE_SUCCESS);
        }else {
            return R.error(SimpleCode.REMOVE_ERROR);
        }
    }

}

